package com.xhui.vimarket.zkplus.zul.attr.mod.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.Spring;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Div;
import org.zkoss.zul.Label;
import org.zkoss.zul.Span;
import org.zkoss.zul.Toolbarbutton;

import com.xhui.vimarket.common.utils.springplus.ApplicationContextUtil;
import com.xhui.vimarket.common.utils.vo.RetVO;
import com.xhui.vimarket.zkplus.utils.ComposerUtil;
import com.xhui.vimarket.zkplus.utils.ZkUiUtil;
import com.xhui.vimarket.zkplus.zul.attr.AttrExt;
import com.xhui.vimarket.zkplus.zul.attr.ada.AttrExecListener;
import com.xhui.vimarket.zkplus.zul.attr.bean.AttrInst;
import com.xhui.vimarket.zkplus.zul.attr.bean.AttrTmpl;
import com.xhui.vimarket.zkplus.zul.attr.bean.ValiVo;
import com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp;
import com.xhui.vimarket.zkplus.zul.attr.rule.AttrCheckRight;
import com.xhui.vimarket.zkplus.zul.attr.util.AttrConstants;

/**
 * @版权：福富软件 版权所有 (c) 2010
 * @文件：com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp.java
 * @所含类：
 * @author: wuyx
 * @version: V1.0
 * @see:
 * @创建日期：2010-12-20
 * @功能说明：属性控件接口
 * @修改记录： =============================================================<br>
 *        日期:2010-12-20 创建
 *        =============================================================<br>
 */
public class AttrBaseComp implements AttrComp {
    
    /**
     * 控件类型标签.
     */
    public static final String  COMP_MODE          = "cp_type";
    /**
     * 控件标签.
     */
    public static final String  COMP_COMP_NAME     = "cp_cname";
    
    /**
     * 控件样式标签.
     */
    public static final String  COMP_STYLE         = "cp_style";
    /**
     * 必须采集，不可空标签.
     */
    public static final String  COMP_CANTNULL      = "cp_cantnull";
    /**
     * 参数选择.
     */
    public static final String  COMP_ARG           = "cp_arg";
    /**
     * 长度标签.
     */
    public static final String  COMP_VALUE_LENGTH  = "cp_vallength";
    /**
     * 最小取值.
     */
    public static final String  COMP_VALUE_FROM    = "cp_valfrom";
    /**
     * 最大取值.
     */
    public static final String  COMP_VALUE_TO      = "cp_valto";
    /**
     * 正则.
     */
    public static final String  COMP_VALUE_REX     = "cp_valrex";
    /**
     * COMP_ATTR_INSTID.
     */
    public static final String  COMP_ATTR_INSTID   = "cp_attr_instid";
    /**
     * COMP_ATTRID.
     */
    public static final String  COMP_IDX           = "cp_idx";
    /**
     * COMP_ATTRID.
     */
    public static final String  COMP_ATTRID        = "cp_attrid";
    /**
     * COMP_GROUPID.
     */
    public static final String  COMP_GROUPID       = "cp_grpid";
    /**
     * COMP_JAVACODE.
     */
    public static final String  COMP_JAVACODE      = "cp_javacode";
    /**
     * COMP_COMPCODE.
     */
    public static final String  COMP_COMPCODE      = "cp_compcode";
    /**
     * COMP_ATTRTMPL.
     */
    public static final String  COMP_ATTRTMPL      = "cp_attrtmpl";
    /**
     * COMP_PARAM1.
     */
    public static final String  COMP_PARAM1        = "cp_param1";
    /**
     * COMP_PARAM1.
     */
    public static final String  COMP_PARAM2        = "cp_param2";
    /**
     * COMP_DATA.
     */
    public static final String  COMP_DATA          = "cp_data";
    /**
     * COMP_ATTRID.
     */
    public static final String  COMP_REMOVED       = "cp_removed";
    /**
     * COMP_OLD_VAL.
     */
    public static final String  COMP_OLD_VAL       = "cp_old_val";
    /**
     * COMP_DEFPO_ID.
     * 默认销售品ID
     */
    public static final String  COMP_DEFPO_ID      = "cp_defpo_id";
    /**
     * .
     */
    public static final int     LCT_TOTAL_WIDTH    = 100;
    /**
     * .
     */
    public static final int     LCT_LABEL_WIDTH    = 40;
    /**
     * .
     */
    public static final int     LCT_TOOLBAR_WIDTH  = 5;
    
    /**
     * 采用自动换行.
     */
    public static final int     SORT_BY_ORDER      = 1;
    
    /**
     * 采用xy坐标进行布局.
     */
    public static final int     SORT_BY_XY         = 0;
    
    /**
     * .
     */
    public static final String  WIDTH_TEXT         = "80%";
    
    /**
     * .
     */
    public static final String  WIDTH_SELECT       = "81%";
    
    /**
     * .
     */
    public static final String  WIDTH_BTNTEXT      = "80%";
    
    /**
     * 无权限信息展示.
     */
    public static final String  VIEW_NONE          = "******";
    
    /**
     * 属性可编辑.
     */
    public static final Integer VIEW_TYPE_EDIT     = 12;
    
    /**
     * 属性可显示但不可编辑.
     */
    public static final Integer VIEW_TYPE_SHOW     = 10;
    /**
     * 属性不可见 打*******.
     */
    public static final Integer VIEW_TYPE_NONE     = 0;
    /**
     * 不可拷贝.
     */
    public static final String  CNS_TYPE_EXTRA_NCP = "NCP";
    
    /**
     * 分割符.
     */
    public static final String  SPLIT_FLAG         = "#";
    
    /** 
     * .
     */
    /**
     * 实例.
     */
    private static AttrBaseComp inst               = new AttrBaseComp();
    
    /**
     * 获取实例.
     * 
     * @return AttrBaseComp
     */
    public static AttrBaseComp instance() {
        return AttrBaseComp.inst;
    }
    
    /**
     * 生成组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件模板
     * @param comp 显示容器组件
     */
    public void create(final AttrExt attrExt, final AttrTmpl attrTmpl, final Component comp) {
    }
    
    /**
     * 生成组件后触发.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件模板
     * @param comp 显示容器组件
     */
    public void afterCreate(final AttrExt attrExt, final AttrTmpl attrTmpl, final Component comp) {
        if (comp != null) {
            AttrBaseComp.dispCheckRight(attrTmpl, comp);
        }
    }
    
    /**
     * 获取值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @return ArrayList<AttrInst>
     */
    public ArrayList<AttrInst> getValue(final AttrExt attrExt, final Component comp) {
        return null;
    }
    
    /**
     * 获取空值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @return ArrayList<AttrInst>
     */
    public ArrayList<AttrInst> getNullValue(final AttrExt attrExt, final Component comp) {
        return null;
    }
    
    
    /**
     * 设置值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param attrInst 属性实例
     * @param attrViewMap 信息权限列表 控制框架的信息
     */
    public void setValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInst, Map<String, Integer> attrViewMap) {
    }

    public void setRemovedValue(final AttrExt attrExt, final Component comp,
            final List<AttrInst> attrInst) {
        }
        
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #setRemovedValue(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component, java.util.List)
     * @author wuyx
     * 2011-4-8 wuyx
     */
    public void setDisabledValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInst) {
    }
    
    @Override
    public ArrayList<AttrInst> getNullValue(final AttrExt attrExt, final AttrTmpl attrTmpl) {
        return null;
    }
    
    /**
     * 清空值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     */
    @Override
    public void clean(final AttrExt attrExt, final Component comp) {
    }
    
    /**
     * getMaxxy.
     * 
     * @param attrExt AttrExt
     * @param attrTmpl AttrTmpl
     * @return int[]
     */
    public static int[] getMaxxy(final AttrExt attrExt, final AttrTmpl attrTmpl) {
        final int[] xy = new int[2];
        xy[0] = 0;
        xy[1] = 0;
        final int sum = attrTmpl.getAttrTmplListCount();
        for (int i = 0; i < sum; i++) {
            final AttrTmpl tAttrTmpl = attrTmpl.getAttrTmplList(i);
            if (tAttrTmpl.getXAddr() > xy[0]) {
                xy[0] = tAttrTmpl.getXAddr();
            }
            if (tAttrTmpl.getYAddr() > xy[1]) {
                xy[1] = tAttrTmpl.getYAddr();
            }
        }
        // 如果采用上级行列为准，则行包含列数直接采用配置数据
        if (AttrBaseComp.SORT_BY_ORDER == attrExt.getAttrAda().getSortType()) {
            xy[0] = attrTmpl.getXAddr();
        }
        return xy;
    }
    
    /**
     * getAttrInstListById.
     * 
     * @param allList List<AttrInst> 
     * @param attrId long
     * @return ArrayList<AttrInst>
     */
    public static ArrayList<AttrInst> getAttrInstListById(final List<AttrInst> allList,
        final long attrId) {
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        final int sum = allList == null ? 0
            : allList.size();
        for (int i = 0; i < sum; i++) {
            final AttrInst attrInst = allList.get(i);
            if (attrInst.getAttrId() == attrId) {
                retList.add(attrInst);
            }
        }
        return retList;
    }
    
    /**
     * 重新生成下级组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件根
     * @param newSubAttrTmplList 组件模板
     * @param comp 本 控件容器组件
     * @author wuyx
     * 传入本级控件根目录，替换新的模板定义，并刷新界面显示
     */
    public void reCreateSub(final AttrExt attrExt, AttrTmpl attrTmpl,
            final List<AttrTmpl> newSubAttrTmplList, final Component comp) {
    }
    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #getComp(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2011-1-24 wuyx
     */
    @Override
    public List<Component> getComp(final AttrExt attrExt, final Component comp, final long attrId) {
        return null;
    }
    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #getComp(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2011-1-24 wuyx
     */
    @Override
    public List<Component> getComp(final AttrExt attrExt, final Component comp,
        final String javaCode) {
        return null;
    }
    
    /**
     * @see com.ffcs.zkplus.zul.attr.mod.AttrComp#setValueInstId
     * (com.ffcs.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component, long)
     * @param attrExt 组件控件
     * @param comp 组件模板
     * @param attrInstId attrInstId
     */
    @Override
    public void setValueInstId(final AttrExt attrExt, final Component comp, final long attrInstId) {
        comp.setAttribute(AttrBaseComp.COMP_ATTR_INSTID, attrInstId);
    }
    
    /** 
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp#setValueInstData
     * (com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component, java.lang.Object)
     * @param attrExt 组件控件
     * @param comp 组件模板
     * @param data Object
     */
    @Override
    public void setValueInstData(final AttrExt attrExt, final Component comp, final Object data) {
        comp.setAttribute(AttrBaseComp.COMP_DATA, data);
    }
    
    /**
     * 生成公用的工具栏.
     * @param parentComp Component
     * @param isVisible boolean
     * @param attrTmpl AttrTmpl
     * @return Div
     * @author wuyx
     * 2011-2-11 wuyx 
     */
    @Override
    public Div createToolbox(final Component parentComp, final boolean isVisible,
        final AttrTmpl attrTmpl) {
        Div div = new Div();
        div.setParent(parentComp);
        Toolbarbutton toolbarbutton = new Toolbarbutton();
        toolbarbutton.setLabel("?");
        toolbarbutton.setVisible(isVisible);
        toolbarbutton.setParent(div);
        toolbarbutton.setAttribute("toolbar", true);
        toolbarbutton.setAttribute("op", "UPDATE");
        EventListener eListener = new EventListener() {
            public void onEvent(final Event event) throws Exception {
                if (event != null && event.getTarget() != null) {
                    Component btncomp = event.getTarget();
                    Object oAttrExt = ComposerUtil.getSupExt(btncomp, AttrExt.class);
                    AttrExt attrExt = oAttrExt == null ? null
                        : (AttrExt) oAttrExt;
                    if (attrExt != null) {
                        attrExt.onToolClick((Toolbarbutton) btncomp);
                    }
                }
            }
        };
        toolbarbutton.addEventListener(Events.ON_CLICK, eListener);
        // AttrExt attrExt = ComposerUtil.getSupExt(comp, clz)
        // toolbarbutton.addForward(orgEvent, target, targetEvent)\
        div.setAttribute("toolbar", true);
        div.setAttribute("op", "UPDATE");
        Div divsub = new Div();
        divsub.setParent(div);
        return div;
    }
    
    /**
     * 生成公用的信息栏.
     * @param attrExt AttrExt
     * @param parentComp Component
     * @param attrTmpl AttrTmpl
     * @return Div
     * @author wuyx
     * 2011-2-11 wuyx 
     */
    public Span createRulebar(final AttrExt attrExt, final Component parentComp,
        final AttrTmpl attrTmpl) {
        Span span = new Span();
        span.setParent(parentComp);
        span.setSclass("zp-lt-label");
        if (attrExt != null && attrExt.getEntity() != null && attrExt.getAttrAda() != null
            && attrExt.getAttrAda().isDispRulebar() && attrTmpl != null
            && attrTmpl.isCantNull(attrExt.getEntity())) { // 不可空
            Label label = new Label();
            label.setValue(" *");
            label.setStyle("color:red;");
            label.setParent(span);
        }
        return span;
    }
    
    /**
     * getToolbarOp.
     * 
     * @param parentComp Component
     * @return String
     * @author wuyx
     * 2011-3-1 wuyx 
     */
    @SuppressWarnings("unchecked")
    public String getToolbarOp(final Component parentComp) {
        if (parentComp != null) {
            final List<Component> subComps = parentComp.getChildren();
            if (subComps != null) {
                for (final Component subComp : subComps) {
                    if (subComp.hasAttribute("toolbar")) { // 工具按钮
                        return subComp.getFirstChild().getAttribute("op") + "";
                    }
                }
            }
        }
        return null;
    }
    
    /**
     * 设置为不允许修改.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param tflag 设置值，true/false
     */
    @Override
    public void setDisabled(final AttrExt attrExt, final Component comp, final boolean tflag) {
    }
    
    /**
     * 设置为不允许修改.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param tflag 设置值，true/false
     */
    @Override
    public void setVisible(final AttrExt attrExt, final Component comp, final boolean tflag) {
        if(comp != null){
            comp.setVisible(tflag);
        }
    }
    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp#
     * exec(com.xhui.vimarket.zkplus.zul.attr.ada.AttrExecListener)
     * @author wuyx
     * 2011-3-16 wuyx
     */
    @Override
    public void exec(final AttrExt attrExt, final Component comp,
        final AttrExecListener attrExecListener) {
        if (attrExecListener == null) {
            return;
        }
        attrExecListener.onEvent(comp, attrExt);
    }
    
    @Override
    public List<RetVO> validate(final AttrExt attrExt, final Component comp) {
        return null;
    }
    
    @Override
    public void refreshButtonText(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList) {
    }
    
    /**
     * getWidthsLCT.
     * 
     * @param attrTmpl AttrTmpl
     * @return String
     * @author wuyx
     * 2011-3-31 wuyx 
     */
    public String getWidthsLCT(final AttrTmpl attrTmpl) {
        int col = attrTmpl.getWidth() == 0 ? 1
            : attrTmpl.getWidth();
        int labelW = AttrBaseComp.LCT_LABEL_WIDTH / col;
        int toolW = AttrBaseComp.LCT_TOOLBAR_WIDTH;
        int contentW = AttrBaseComp.LCT_TOTAL_WIDTH - labelW - toolW;
        return labelW + "%," + contentW + "%," + toolW + "%";
    }
    
    /**
     * getWidthsLCT.
     * 
     * @param attrTmpl AttrTmpl
     * @return String
     * @author wuyx
     * 2011-3-31 wuyx 
     */
    public String getWidthsLCTS(final AttrTmpl attrTmpl) {
        int col = attrTmpl.getWidth() == 0 ? 1
            : attrTmpl.getWidth();
        int labelW = AttrBaseComp.LCT_LABEL_WIDTH / col;
        int toolW = AttrBaseComp.LCT_TOOLBAR_WIDTH;
        int contentW = AttrBaseComp.LCT_TOTAL_WIDTH - labelW - toolW - (labelW / 3);
        return labelW + "%," + contentW + "%," + toolW + "%";
    }

	 @Override
	 public void setAttrView(Component comp, Map<String, Integer> attrViewMap) {
	     // TODO Auto-generated method stub
	     
	 }
	 
	 @Override
	 public void setAttrConstrain(Component comp, Map<Object, String> attrConstrainMap) {
	     // TODO Auto-generated method stub
	     
	 }
    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #getMsgComp(org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2011-7-19 wuyx
     */
    @Override
    public Component getMsgComp(Component comp) {
        return comp;
    }
    
    @Override
    public ArrayList<ValiVo> signRuleHint(AttrExt attrExt, Component comp) {
        return null;
    }
    
    /**
     * 权限判断.
     * 
     * @param rootComp
     */
    public static boolean dispCheckRight(final AttrTmpl attrTmpl, final Component rootComp) {
        // 是否需要判断权限
        if (rootComp != null && attrTmpl.isNeedCheckRight()) {
            // 判断权限
            AttrCheckRight acr = (AttrCheckRight) ApplicationContextUtil.getBean("attrCheckRight");
            if (acr != null) {
                if (!acr.hasRight(AttrConstants.CHECK_RIGHT_PREFIX + attrTmpl.getAttrId())) {
                    // 没权限，将控件disabled
                    ZkUiUtil.setDisabled(rootComp, true);
                    return false; // 无权限 
                }
            }
        }
        return true; // 有权限 
    }
    
}
